package com.wondersgroup.core.util;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.wondersgroup.core.mapper.DataDictDetailMapper;
import com.wondersgroup.core.mapper.DataDictMapper;
import com.wondersgroup.core.model.DataDict;
import com.wondersgroup.core.model.DataDictDetail;

/**
 * 从数据库加载字典数据的工具类
 * @author wanglei
 *
 */
public class DataDicLoader {

    @Autowired
    private DataDictMapper dataDictMapper;
    @Autowired
    private DataDictDetailMapper dataDictDetailMapper;

    public DataDicLoader() {
    }

    /**
     * 加载字典数据到静态变量
     */
    @Transactional(value = "transactionManager")
    public void init() {
        try {
            Map<Integer, String> nameMap = new LinkedHashMap<Integer, String>();
            Map<String, Map<Integer, String>> typeDetailmap = new LinkedHashMap<>();
            Map<String, Map<Integer, String>> typeRootDetailmap = new LinkedHashMap<>();
            Map<Integer, String> detailNameMap = new LinkedHashMap<Integer, String>();
            Map<Integer, Map<Integer, String>> parentDetailMap = new LinkedHashMap<>();
            Map<Integer, DataDictDetail> detailMap = new LinkedHashMap<Integer, DataDictDetail>();

            try {
                List<DataDict> dataDicts = dataDictMapper.select(null);
                List<DataDictDetail> dataDictDetails = dataDictDetailMapper.selectOrderBy();
                if (dataDicts != null) {
                    for (DataDict dict : dataDicts) {
                        nameMap.put(dict.getId(), dict.getName());
                    }
                }
                if (dataDictDetails != null) {
                    for (DataDictDetail ddd : dataDictDetails) {
                        Integer detailId = ddd.getId();
                        String detailName = ddd.getValue();
                        DataDictDetail parent = dataDictDetailMapper.selectByPrimaryKey(ddd.getParentId());
                        if (ddd.getTypeId() != null && ddd.getTypeId()>0) {
                            Integer typeId = ddd.getTypeId();
                            //Integer comId = ddd.getCompanyId();
                            String key = typeId + ":"; //+ (comId == null?"":comId+"");
                            if (!typeDetailmap.containsKey(key)) {
                            	typeDetailmap.put(key, new LinkedHashMap<Integer, String>());
                            }
                            typeDetailmap.get(key).put(detailId, detailName);
                            //如果是根节点
                            if (parent == null || parent.getId() == null) {
                            	if(!typeRootDetailmap.containsKey(key)){
                            		typeRootDetailmap.put(key, new LinkedHashMap<Integer, String>());
                            	}
                                typeRootDetailmap.get(key).put(detailId, detailName);
                            }
                        }
                        if (parent != null && parent.getId() != null) {
                            Integer parentId = parent.getId();
                            if (!parentDetailMap.containsKey(parentId)) {
                                parentDetailMap.put(parentId, new LinkedHashMap<Integer, String>());
                            }
                            parentDetailMap.get(parentId).put(detailId, detailName);
                        }
                        detailNameMap.put(detailId, detailName);
                        detailMap.put(detailId, ddd);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            DataDicConstant.setDataDicNameMap(nameMap);
            DataDicConstant.setDataDicDetailMap(typeDetailmap);
            DataDicConstant.setDataDicRootDetailMap(typeRootDetailmap);
            DataDicConstant.setDataDicDetailNameMap(detailNameMap);
            DataDicConstant.setDataDicParentDetailMap(parentDetailMap);
            DataDicConstant.setDetailMap(detailMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public DataDictMapper getDataDictMapper() {
        return dataDictMapper;
    }

    public void setDataDictMapper(DataDictMapper dataDictMapper) {
        this.dataDictMapper = dataDictMapper;
    }

    public DataDictDetailMapper getDataDictDetailMapper() {
        return dataDictDetailMapper;
    }

    public void setDataDictDetailMapper(DataDictDetailMapper dataDictDetailMapper) {
        this.dataDictDetailMapper = dataDictDetailMapper;
    }
}
